package com.jinyu.jyly.base;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.Toast;

import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;
import com.jinyu.jyly.utils.DisplayCutoutUtil;

import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import butterknife.ButterKnife;


/**
 * @Description fragment的基类, 并且采用了fragment了懒加载机制  SupportMapFragment
 */
public abstract class BaseFragment<T extends IPresenter> extends Fragment {

    public Activity mContext;
    public T mPresenter;
    private View view;
    private boolean isViewCreated;//页面是否被创建了
    private boolean isUIVisiable; //页面是否可见了
    public LayoutInflater mInflater;
    public Bundle mSavedInstanceState;
    public QMUITipDialog mTipDialog;
    public boolean mIsNeed;


    public static final int REQUEST_CALL_PERMISSION = 10111; //拨号请求码

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.mSavedInstanceState = savedInstanceState;
        mContext = getActivity();
        mPresenter = getPresenter();
        mInflater = LayoutInflater.from(mContext);
        mTipDialog = new QMUITipDialog.Builder(getActivity())
                .setIconType(QMUITipDialog.Builder.ICON_TYPE_LOADING)
                .setTipWord("正在加载")
                .create();

    }


    @Override
    public View onCreateView(LayoutInflater inflater,  ViewGroup container, Bundle savedInstanceState) {
        view = initView(container);
        ButterKnife.bind(this, view);
        isViewCreated = true;
        setStatusBar();
        loadData();
        return view;

    }

    protected void setStatusBar() {
        //适配刘海
        mIsNeed = DisplayCutoutUtil.needAdaptNotch(getActivity());
    }

    protected abstract View initView(ViewGroup container);

    /**
     * 懒加载数据方法
     */
    private void lazyData() {
        if (isUIVisiable && isViewCreated) {
            loadData();
            isUIVisiable = false;
            isViewCreated = false;
        }
    }

    //加载数据
    protected abstract void loadData();

    //获取presenter指示器
    public abstract T getPresenter();



    /**
     * 关闭软键盘
     */
    public void closeKeyboard() {
        View view = mContext.getWindow().peekDecorView();
        if (view != null) {
            InputMethodManager inputMethodManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
            inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    }

    @Override
    public void startActivity(Intent intent) {
        super.startActivity(intent);

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) mPresenter.detachView();
    }


    /**
     * 弹吐司
     */
    public void showToast(String connect){
        Toast.makeText(mContext, connect, Toast.LENGTH_SHORT).show();
    }


    /**
     * 判断是否有某项权限
     * @param string_permission 权限
     * @param request_code 请求码
     * @return
     */
    public boolean checkReadPermission(String string_permission,int request_code) {
        boolean flag = false;
        if (ContextCompat.checkSelfPermission(mContext, string_permission) == PackageManager.PERMISSION_GRANTED) {//已有权限
            flag = true;
        } else {//申请权限
            ActivityCompat.requestPermissions(mContext, new String[]{string_permission}, request_code);
        }
        return flag;
    }


    /**
     * 检查权限后的回调
     * @param requestCode 请求码
     * @param permissions  权限
     * @param grantResults 结果
     */
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
        }
    }





}
